#include "Browser.h"

Browser::Browser(QWidget *parent, const QUrl& url) :
    QMainWindow(parent)
{
    progress = 0;

    QFile file;
    file.setFileName("://jquery.min.js");
    file.open(QIODevice::ReadOnly);
    jQuery = file.readAll();
    jQuery.append("\nvar qt = { 'jQuery': jQuery.noConflict(true) };");
    file.close();

    QNetworkProxyFactory::setUseSystemConfiguration(true);

    view = new QWebView(parent);
    view->load(url);

    connect(view, SIGNAL(loadFinished(bool)), SLOT(adjustLocation()));
    connect(view, SIGNAL(titleChanged(QString)), SLOT(adjustTitle()));
    connect(view, SIGNAL(loadProgress(int)), SLOT(setProgress(int)));
    connect(view, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));

    // Barra de direcciones
    locationEdit = new QLineEdit(this);
    locationEdit->setSizePolicy(QSizePolicy::Expanding, locationEdit->sizePolicy().verticalPolicy());
    connect(locationEdit, SIGNAL(returnPressed()),SLOT(changeLocation()));
    locationEdit->setStyleSheet("background-color: rgb(255, 255, 255)");

    // Action back
    act_back = new QAction(this);
    act_back->setIcon(QIcon("://Icons/arrow_left_icon&24.png"));
    connect(act_back,SIGNAL(triggered()),this,SLOT(ActionBackPage()));

    // Action Forward
    act_forward = new QAction(this);
    act_forward->setIcon(QIcon("://Icons/arrow_right_icon&24.png"));
    connect(act_forward,SIGNAL(triggered()),this,SLOT(ActionForwardPage()));

    // Action Reload
    act_reload = new QAction(this);
    act_reload->setIcon(QIcon("://Icons/playback_reload_icon&24.png"));
    connect(act_reload,SIGNAL(triggered()),this,SLOT(ActionReloadPage()));

    // Action Stop
    act_stop = new QAction(this);
    act_stop->setIcon(QIcon("://Icons/delete_icon&24.png"));
    connect(act_stop,SIGNAL(triggered()),this,SLOT(ActionStopPage()));

    QToolBar *toolBar = addToolBar(tr("Navigation"));
    toolBar->setMovable(false);
    toolBar->setStyleSheet("background-color: rgb(0, 159, 52); border-style: solid");
    toolBar->addAction(act_back);
    toolBar->addAction(act_forward);
    toolBar->addAction(act_reload);
    toolBar->addAction(act_stop);

    toolBar->addWidget(locationEdit);
    locationEdit->setFocus();
    setCentralWidget(view);

    // Barra de búsqueda de google
//    g_search = new SearchBox(this);
//    g_search->setStyleSheet("background-color: rgb(0, 139, 56);color: rgb(255, 255, 255)");

//    QStatusBar *st = new QStatusBar(this);
//    st->addWidget(g_search);
//    setStatusBar(st);
}

void Browser::adjustLocation()
{
    locationEdit->setText(view->url().toString());
}

void Browser::changeLocation()
{
    QString s_url = locationEdit->text();
    QUrl url = QUrl::fromUserInput(s_url);
    locationEdit->setText(s_url);
    view->load(url);
    view->setFocus();
}

void Browser::adjustTitle()
{

}

void Browser::setProgress(int p)
{
    progress = p;
}

void Browser::finishLoading(bool)
{
    progress = 100;
    //adjustTitle();
    view->page()->mainFrame()->evaluateJavaScript(jQuery);
}

void Browser::ActionBackPage()
{
    view->pageAction(QWebPage::Back);
}

void Browser::ActionForwardPage()
{
    view->pageAction(QWebPage::Forward);
}

void Browser::ActionReloadPage()
{
    view->pageAction(QWebPage::Reload);
}

void Browser::ActionStopPage()
{
    view->pageAction(QWebPage::Stop);
}
